package longrise.phone.com.itssnew.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import com.longrise.android.Global;

import org.litepal.crud.DataSupport;

import butterknife.ButterKnife;
import longrise.phone.com.itssnew.bean.CompanyBean;
import longrise.phone.com.itssnew.bean.Constant;
import longrise.phone.com.itssnew.bean.JZSQGloble;
import longrise.phone.com.itssnew.utils.CommonUtil;
import longrise.phone.com.itssnew.utils.RxManager;
import longrise.phone.com.itssnew.utils.SPUtil;
import longrise.phone.com.itssnew.widget.LoadDialog;

public abstract class BaseActivity extends Activity {
    protected RxManager rxManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(bindLayoutId());
        ButterKnife.bind(this);
        rxManager = new RxManager();
    }

    public abstract int bindLayoutId();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        rxManager.clear();
    }

    protected void startActivity(Class activity){
        this.startActivity(new Intent(this,activity));
    }



    /********************登录相关********************************/

    /**
     * 判断登录是否失效,联网前调用
     */
    protected boolean loginInvalid(Activity activity){
        if(null == Global.getInstance().getUserInfo()){
            showLoginInvalidDialog(activity);
            return true;
        }return  false;
    }


    /**
     * 提示分数没保存
     * @param activity
     */
    protected void showScoreNotSaveDialog(final Activity activity){
        CommonUtil.showDialog(this, "请检查您的评分数据是否保存，点击确定返回企业列表", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });

    }


    /**
     * 登录失效，是否重新登录
     * @param activity
     */
    protected void showLoginInvalidDialog(final Activity activity){
        CommonUtil.showDialog(this, "由于您长时间未操作，登录失效，是否重新登录？", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
                startActivity(LoginActivity.class);
            }
        });

    }

    protected void showLoginOutDialog(final Activity activity){
        CommonUtil.showDialog(this, "确认注销当前用户？", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginOut(activity);
            }
        });

    }

    private void loginOut(final Activity activity){
        final Dialog mDialog=new LoadDialog(this);
        mDialog.show();
        endHeartbeat();
        JZSQGloble.getInstance(this).call("logout", Object.class, new JZSQGloble.OnCallFinishListener() {
            @Override
            public void onCallFinish(Object bean) {
                mDialog.dismiss();
                if(bean == null) return;
                clearInformation();
                activity.finish();
                startActivity(LoginActivity.class);
            }
        });
    }

    private void clearInformation() {
        DataSupport.deleteAll(CompanyBean.class);
        //SPUtil.setSharedIntData(this, Constant.SPOPERATIONSORDESIGN,0);
        //SPUtil.setSharedBooleanData(this,Constant.REMEMBERPASSWORD,false);
        SPUtil.setSharedBooleanData(this,Constant.AUTOLOGIN,false);
    }

    private static Thread heartbeatThread =null;
    public void startHeartbeat(){
        if(heartbeatThread==null){
            heartbeatThread =new Thread(heartbeatRun);
        }
        if(!heartbeatThread.isAlive()){
            try {
                heartbeatThread.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void endHeartbeat(){
        if(heartbeatThread!=null){
            if(heartbeatThread.isAlive()){
                try {
                    heartbeatThread.interrupt();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public Boolean  Heartbeat(){
        System.out.println("心跳------------------------");
        Boolean resultSet = (Boolean) Global.getInstance().call("Heartbeat", Boolean.class);
        return resultSet;
    }

    Runnable heartbeatRun = new Runnable() {

        @Override
        public void run() {

            while(!Thread.currentThread().isInterrupted()){
                Heartbeat();
                try {
                    Thread.sleep(60*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    };
}
